package com.icongyou.enterprise.talent_analysis.event;

import com.icongyou.enterprise.talent_analysis.service.AIInsightCacheService;
import com.icongyou.enterprise.talent_analysis.service.PortraitAsyncService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 学生数据变更事件监听器
 * 当学生数据变更时，自动触发画像更新和AI洞察缓存失效
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class StudentDataChangeListener {

    private final PortraitAsyncService portraitAsyncService;
    private final AIInsightCacheService aiInsightCacheService;

    @EventListener
    @Async
    public void handleDataChange(StudentDataChangeEvent event) {
        
        // 1. 触发画像更新
        portraitAsyncService.triggerStudentPortraitUpdate(
                event.getStudentId(), 
                event.getChangeType() + ":" + event.getSourceId()
        );
        
        // 2. 使AI洞察缓存失效（下次访问时重新生成）
        aiInsightCacheService.invalidateStudentCache(event.getStudentId());
        
    }
}
